/**
 * 
 */
package com.epart.widget.module.login;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.epart.widget.domain.AdminAdministrator;
import com.epart.widget.domain.Member;
import com.epart.widget.support.AbstractWcmsWidgetDao;
import com.epart.widget.util.DBUtil;

/**
 * 로그인Dao
 * 
 * @author cwsong
 * @version 2011. 3. 10.
 */
public class LoginDao extends AbstractWcmsWidgetDao {

	/**
	 * 커넥션을 인자로 받는 생성자
	 * 
	 * @param connection
	 */
	public LoginDao(Connection connection) {
		this.connection = connection;
	}

	/**
	 * 회원정보를 반환한다.
	 * 
	 * @param memberId
	 * @return 회원정보
	 */
	public Member getMember(String memberId) {
		StringBuilder sb = new StringBuilder(1000);
		sb.append(" SELECT ");
		sb.append(" * ");
		sb.append(" FROM ");
		sb.append(" cms_member ");
		sb.append(" WHERE ");
		sb.append(" memberId = ? ");

		PreparedStatement psmt = null;
		ResultSet rs = null;
		Member member = null;

		try {
			psmt = connection.prepareStatement(sb.toString());
			psmt.setString(1, memberId);
			rs = psmt.executeQuery();

			if (rs.next()) {
				member = new Member();
				member.setMemberNo(rs.getInt("memberNo"));
				member.setMemberId(rs.getString("memberId"));
				member.setMemberPassword(rs.getString("memberPassword"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.doFinally(rs, psmt, connection);
		}
		return member;
	}

	/**
	 * 관리자정보를 반환한다.
	 * 
	 * @param adminId
	 * @return 관리자정보
	 */
	public AdminAdministrator getAdmin(String adminId) {

		StringBuilder sb = new StringBuilder(1000);
		sb.append(" SELECT ");
		sb.append(" * ");
		sb.append(" FROM ");
		sb.append(" admin_administrator ");
		sb.append(" WHERE ");
		sb.append(" adminId = ? ");

		PreparedStatement psmt = null;
		ResultSet rs = null;
		AdminAdministrator admin = null;

		try {
			psmt = connection.prepareStatement(sb.toString());
			psmt.setString(1, adminId);

			rs = psmt.executeQuery();

			if (rs.next()) {
				admin = new AdminAdministrator();
				admin.setAdminId(rs.getString("adminId"));
				admin.setAdminPassword(rs.getString("adminPassword"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.doFinally(rs, psmt, connection);
		}
		return admin;
	}
}
